import 'package:faker/common/application.dart';
import 'package:faker/events/updatearticle.dart';
import 'package:faker/i18n/i18n.dart';
import 'package:faker/models/local_rss.dart';
import 'package:faker/models/rss.dart';
import 'package:faker/screen/rss_details_screen.dart';
import 'package:faker/utils/route_util.dart';
import 'package:flutter/material.dart';
import 'package:flutter_slidable/flutter_slidable.dart';

class ItemRssList extends StatefulWidget {
  final LocalRss item;

  /// 滑动删除Controller
  SlidableController slidableController;

  /// 编辑
  Function edit;

  /// 删除TODO
  Function delete;

  ItemRssList(
      {Key key,
      @required this.item,
      @required this.edit,
      @required this.slidableController,
      @required this.delete})
      : super(key: key);

  @override
  State<StatefulWidget> createState() {
    return new ItemRssListState();
  }
}

class ItemRssListState extends State<ItemRssList> {
  int cnt;

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    this.registerUpdateArticleEvent();
    print("Init");
    cnt = 0;
  }

  void update() async {
    print("update start");
    RssBean rssFeed = await RssModel().getRss(widget.item.route);
    setState(() {
      cnt = rssFeed.items.length;
      print("update end:" + cnt.toString());
    });
  }

  /// 注册改变事件
  void registerUpdateArticleEvent() async {
    Application.eventBus
        .on<UpdateArticleEvent>()
        .listen((event) => this.update());
  }

  @override
  Widget build(BuildContext context) {
    return Material(
      color: Theme.of(context).primaryColorLight,
      child: Slidable(
        controller: widget.slidableController,
        actionPane: SlidableDrawerActionPane(),
        actionExtentRatio: 0.25,
        child: InkWell(
          onTap: () {
            RouteUtil.push(
                context,
                RssDetail(
                  route: widget.item.route,
                  title: widget.item.title,
                ));
          },
          child: Column(
            children: <Widget>[
              Row(
                children: <Widget>[
                  Expanded(
                    child: Container(
                      padding: EdgeInsets.fromLTRB(16, 8, 16, 8),
                      child: Column(
                        children: <Widget>[
                          Container(
                            alignment: Alignment.topLeft,
                            padding: EdgeInsets.fromLTRB(0, 2, 0, 2),
                            child: Text(
                              widget.item.title,
                              style: TextStyle(
                                  fontSize: 16,
                                  color: Theme.of(context).textSelectionColor),
                              maxLines: 1,
                              overflow: TextOverflow.ellipsis,
                              textAlign: TextAlign.left,
                            ),
                          ),
                          Container(
                            alignment: Alignment.topLeft,
                            padding: EdgeInsets.fromLTRB(0, 8, 0, 2),
                            child: Row(
                              crossAxisAlignment: CrossAxisAlignment.center,
                              children: <Widget>[
                                Text(
                                  widget.item.updateDate,
                                  style: TextStyle(
                                      fontSize: 15, color: Color(0xFF757575)),
                                  maxLines: 3,
                                  overflow: TextOverflow.ellipsis,
                                  textAlign: TextAlign.left,
                                ),
                                SizedBox(
                                  width:
                                      MediaQuery.of(context).size.width - 160,
                                ),
                                Text(
                                  cnt.toString(),
                                  style: TextStyle(
                                      fontSize: 15, color: Color(0xFF757575)),
                                )
                              ],
                            ),
                          ),
                        ],
                      ),
                    ),
                  ),
                ],
              ),
              Divider(height: 1),
            ],
          ),
        ),
        secondaryActions: <Widget>[
          IconSlideAction(
            caption: S.of(context).delete,
            color: Theme.of(context).backgroundColor,
            icon: Icons.delete,
            onTap: () {
              widget.delete(widget.item.id);
            },
          ),
          IconSlideAction(
            caption: S.of(context).edit,
            color: Theme.of(context).backgroundColor,
            icon: Icons.edit,
            onTap: () {
              widget.edit(widget.item);
            },
          ),
        ],
      ),
    );
  }
}
